<template>
  <view>
    <my-search @click="gotoSearch"></my-search>
    <!-- 轮播图区域 uSwiper -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-list">
      <!-- 绑定点击事件 -->
      <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
        <image class="nav-img" :src="item.image_src"></image>
      </view>
    </view>
    <!-- #4 楼层区域 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item, i1) in floorList" :key="i1">
        <!-- mode="widthFix" 宽度固定，高度自适应 -->
        <!-- 楼层标题 -->
        <image class="floor-title" :src="item.floor_title.image_src" :alt="item.floor_title.name" />
        <!-- 楼层内容 -->
        <view class="floor-img-box">
          <!-- 左侧大图片的盒子 -->
          <navigator class="left-img-box" :url="item.product_list[0].url">
            <image mode="widthFix" :style="{ width: item.product_list[0].image_width + 'rpx' }"
              :src="item.product_list[0].image_src">
            </image>
          </navigator>
          <!-- 右边四个图片的盒子 -->
          <view class="right-img-box">
            <!-- 每一个图片的包裹 -->
            <!-- v-for 和 v-if 放一行性能不好 -->
            <!-- <view class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 !== 0"> -->
            <navigator :url="item2.url" class="right-img-item" v-for="(item2, i2) in item.product_list.slice(1)"
              :key="i2">
              <image mode="widthFix" :src="item2.image_src" :style="{ width: item2.image_width + 'rpx' }"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import setBadge from '../../mixins/tabbar-badge.js'
  export default {
    data() {
      return {
        swiperList: [],
        navList: [],
        // #1
        floorList: []
      };
    },
    mixins: [setBadge],
    onLoad() {
      this.loadSwiperList()
      this.getNavList()
      // #2
      this.getFloorList()
    },
    methods: {
      async loadSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/home/swiperdata')
        if (res.meta.status !== 200) return uni.$showMsg()
        // 这儿直接像 Vue 那样赋值就行了，uni-app 最终会把下面的代码还是编译成小程序的语法，类似于 setData 的形式
        this.swiperList = res.message
      },
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },
      // #3
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg()
        // navigator_url => /pages/goods_list/index?query=服饰
        // '/subpkg/goods_list/goods_list?' + navigator_url.split('?')[1]
        // navigator_url => /subpkg/goods_list/goods_list?query=服饰
        res.message.forEach(floor => {
          floor.product_list.forEach(cate => {
            cate.url = '/subpkg/goods_list/goods_list?' + cate.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      navClickHandler(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 32rpx 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-title {
    height: 60rpx;
    width: 100%;
  }

  .floor-img-box {
    display: flex;
    padding-left: 10rpx;
  }

  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
</style>
